package com.dailyblue.java.spring.boot.boot01.controller;

import com.dailyblue.java.spring.boot.boot01.bean.Emp;
import com.dailyblue.java.spring.boot.boot01.bean.JsonResult;
import com.dailyblue.java.spring.boot.boot01.service.EmpService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

// restful风格写法：get请求---查询 delete请求---删除 post请求---添加  put请求---修改
@RestController
@RequestMapping("/emp")
public class EmpController {
    @Resource
    private EmpService empService;

    @GetMapping(produces = {"application/json;charset=utf-8"})
    public JsonResult<List<Emp>> find() {
        System.out.println("find");
        return empService.findAllEmp();
    }
    // http://localhost:8081/emp/a?page=1
    // http://localhost:8081/emp/1
    @GetMapping(value = "/{page}", produces = {"application/json;charset=utf-8"})
    public JsonResult<PageInfo<Emp>> find(@PathVariable("page") int page) {
        return empService.findEmpByPage(page, 5);
    }

    @DeleteMapping
    public void delete(int id) {
        System.out.println("delete");
        empService.deleteEmp(id);
    }

    @PostMapping
    public JsonResult<List<Emp>> save(Emp emp) {
        return empService.saveEmp(emp);
    }

    @PutMapping
    public JsonResult<List<Emp>> update(Emp emp) {
        return empService.updateEmp(emp);
    }
}
